package org.jeecg.modules.demo.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import liquibase.pro.packaged.L;
import org.jeecg.modules.demo.base.entity.*;
import org.jeecg.modules.demo.base.mapper.ZnzfEmployeeMapper;
import org.jeecg.modules.demo.base.mapper.ZnzfEmployeeScoreItemMapper;
import org.jeecg.modules.demo.base.mapper.ZnzfScoreStandardItemMapper;
import org.jeecg.modules.demo.base.service.IZnzfEmployeeScoreItemService;
import org.jeecg.modules.demo.checkemployee.entity.ZnzfCheckEmployee;
import org.jeecg.modules.demo.checkemployee.mapper.ZnzfCheckEmployeeMapper;
import org.jeecg.modules.demo.single.entity.ZnzfNeedSingle;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.mapper.SysUserMapper;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 职工计分包含的标准项
 * @Author: jeecg-boot
 * @Date:   2022-03-29
 * @Version: V1.0
 */
@Service
@Transactional
public class ZnzfEmployeeScoreItemServiceImpl extends ServiceImpl<ZnzfEmployeeScoreItemMapper, ZnzfEmployeeScoreItem> implements IZnzfEmployeeScoreItemService {

    @Resource
    private ZnzfEmployeeScoreItemMapper znzfEmployeeScoreItemMapper;

    @Resource
    private ZnzfEmployeeMapper znzfEmployeeMapper;

    @Resource
    private SysUserMapper sysUserMapper;

    @Resource
    private ZnzfScoreStandardItemMapper znzfScoreStandardItemMapper;

    @Resource
    private ZnzfCheckEmployeeMapper znzfCheckEmployeeMapper;

    /**
     * 添加职工所有的计分标准项
     * @param znzfEmployeeScoreItemParamList
     */
    @Override
    public void insertAllSocreItem(List<ZnzfEmployeeScoreItemParam> znzfEmployeeScoreItemParamList) {

        for (int i = 0; i < znzfEmployeeScoreItemParamList.size(); i++) {
            ZnzfEmployeeScoreItemParam item = znzfEmployeeScoreItemParamList.get(i);
            ZnzfEmployeeScoreItem tmp = new ZnzfEmployeeScoreItem();
            tmp.setScoreItemId(item.getScoreItemId());
            tmp.setUserId(item.getUserId());
            tmp.setScore(item.getScore());
            tmp.setAppendix(item.getAppendix());
            // 设置状态，
            tmp.setState(1+"");
            znzfEmployeeScoreItemMapper.insert(tmp);
        }

    }

    /**
     * 查看所有的职工计分标准项信息
     * @return
     */
    @Override
    public List<ZnzfEmployeeScoreItem> queryAllEmployeeScoreItemList() {
        LambdaQueryWrapper<ZnzfEmployeeScoreItem> queryWrapper = new LambdaQueryWrapper<>();
        List<ZnzfEmployeeScoreItem> list = znzfEmployeeScoreItemMapper.selectList(queryWrapper);
        List<ZnzfEmployeeScoreItem> arrayList = new ArrayList<>();
        for (int i = 0; i < list.size(); i ++) {
            ZnzfEmployeeScoreItem znzfEmployeeScoreItem = list.get(i);
            SysUser sysUser = sysUserMapper.selectById(znzfEmployeeScoreItem.getUserId());
            if (sysUser != null) {
                znzfEmployeeScoreItem.setUserName(sysUser.getRealname());
            }
            ZnzfScoreStandardItem znzfScoreStandardItem = znzfScoreStandardItemMapper.selectById(znzfEmployeeScoreItem.getScoreItemId());
            if (znzfScoreStandardItem != null) {
                znzfEmployeeScoreItem.setSSIName(znzfScoreStandardItem.getName());
                znzfEmployeeScoreItem.setSSIScore(znzfScoreStandardItem.getScore());
                znzfEmployeeScoreItem.setSSINote(znzfScoreStandardItem.getNote());
            }
            arrayList.add(znzfEmployeeScoreItem);
        }
        return arrayList;
    }


    /**
     * 添加职工所有的计分标准项
     * @param znzfEmployeeScoreItemParam
     */
    @Override
    public int insertAllSocreItem2(ZnzfEmployeeScoreItemListParam znzfEmployeeScoreItemParam) {

        System.out.println(znzfEmployeeScoreItemParam.toString());
        for (int i = 0; i < znzfEmployeeScoreItemParam.getZnzfEmployeeScoreItemParamList().size(); i++) {
            ZnzfEmployeeScoreItemParam item =znzfEmployeeScoreItemParam.getZnzfEmployeeScoreItemParamList().get(i);

            LambdaQueryWrapper<ZnzfEmployeeScoreItem> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ZnzfEmployeeScoreItem::getScoreItemId, item.getScoreItemId())
                        .eq(ZnzfEmployeeScoreItem::getUserId, item.getUserId());
            ZnzfEmployeeScoreItem znzfEmployeeScoreItem = znzfEmployeeScoreItemMapper.selectOne(queryWrapper);

            if (znzfEmployeeScoreItem == null) {
                ZnzfEmployeeScoreItem tmp = new ZnzfEmployeeScoreItem();
                tmp.setScoreItemId(item.getScoreItemId());
                tmp.setUserId(item.getUserId());
                tmp.setScore(item.getScore());
                tmp.setAppendix(item.getAppendix());
                // 设置状态，
                tmp.setState(1+"");
                znzfEmployeeScoreItemMapper.insert(tmp);
            } else {
                if (Integer.parseInt(znzfEmployeeScoreItem.getState()) != 0) {
                    znzfEmployeeScoreItem.setAppendix(item.getAppendix());
                    znzfEmployeeScoreItem.setState(1+"");
                    znzfEmployeeScoreItemMapper.updateById(znzfEmployeeScoreItem);
                }
            }
        }
        return 1;
    }

    /**
     * 根据userId插入总分
     * @param userId
     * @param latestScore
     * @return
     */
    @Override
    public int insertLatestScoreByUserId(String userId, String latestScore) {
        LambdaQueryWrapper<ZnzfEmployee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZnzfEmployee::getUserId, userId);
        ZnzfEmployee znzfEmployee = znzfEmployeeMapper.selectOne(queryWrapper);
        znzfEmployee.setLatestScore(Double.parseDouble(latestScore));
        znzfEmployeeMapper.updateById(znzfEmployee);

        LambdaQueryWrapper<ZnzfCheckEmployee> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(ZnzfCheckEmployee::getUserId, userId);
        List<ZnzfCheckEmployee> employeeList = znzfCheckEmployeeMapper.selectList(queryWrapper1);
        employeeList.forEach(employee->{
            employee.setScore(Double.parseDouble(latestScore));
            znzfCheckEmployeeMapper.updateById(employee);
        });
        return 1;
    }

    /**
     * 根据userId查找总分
     * @param userId
     * @return
     */
    @Override
    public Double queryLatestScoreByUserId(String userId) {
        LambdaQueryWrapper<ZnzfEmployee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZnzfEmployee::getUserId, userId);
        ZnzfEmployee znzfEmployee = znzfEmployeeMapper.selectOne(queryWrapper);
        if (znzfEmployee != null) {
            return znzfEmployee.getLatestScore();
        }
        return null;
    }


    /**
     * 查看职工的资质历史信息
     * @param userId
     * @return
     */
    @Override
    public List<ZnzfEmployeeScoreItem> queryEmployeeScoreItemListByUid(String userId) {
        LambdaQueryWrapper<ZnzfEmployeeScoreItem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZnzfEmployeeScoreItem::getUserId, userId);
        List<ZnzfEmployeeScoreItem> itemList  = znzfEmployeeScoreItemMapper.selectList(queryWrapper);
        return itemList;
    }
}
